//
//  HistoryViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/5.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "HistoryViewController.h"
#import "CurveViewController.h"
#import "CurveListViewController.h"
#import "DabiaoViewController.h"
#import "BGSlideView.h"


@interface HistoryViewController ()
{
    NSString *_jinRiDaBiao1;
    NSString *_jinRiDaBiao2;

    NSString *_jinRiTiCheng1;
    NSString *_jinRiTiCheng2;
    BOOL _isShowHidden;

}
@property (nonatomic,strong) BGSlideView *slideView;

@end

@implementation HistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
    [self loadData];
}

- (void)UISetting{
    _isShowHidden = YES;
    _layoutWidth.constant = kScreenSize.width;
    _layoutHeight.constant = 670;
//    _layoutHeight.constant = 686;

    
//    [self setViewUIByView:_viewJinRi];
    [self setViewUIByView:_viewWoDeChongdianbao];
    [self setViewUIByView:_viewDaBiao];
    [self setViewUIByView:_viewTiCheng];
    
    _labelDabiaoBenyue.text = [NSString stringWithFormat:@"%@月累计达标",[BGDateHelper seeDay][2]];
    
    _labelTiChengBenyue.text = [NSString stringWithFormat:@"%@月累计提成",[BGDateHelper seeDay][2]];
}

- (void)setViewUIByView:(UIView *)view{
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 5.f;
    
//    view.layer.shadowColor = [kBlackColor CGColor];
//    view.layer.shadowOffset = CGSizeMake(1, 1);
//    view.layer.shadowRadius = 1;
//    view.layer.shadowOpacity = 0.3;
}


- (void)creatSideViewByContent:(NSString *)content{
    if (!_slideView) {
        _slideView = [[BGSlideView alloc] initWithOrginY:69 content:content];
        [self.view addSubview:_slideView];
    }else{
        [_slideView removeFromSuperview];
        _slideView = nil;
        _slideView = [[BGSlideView alloc] initWithOrginY:69 content:content];
        [self.view addSubview:_slideView];
    }
}


#pragma mark - 网络请求

- (void)loadData{
    [self.hub hideAnimated:YES];
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    
    [[AFClient shareInstance] tongjiByEid:kUserId date:[BGDateHelper seeDay][0] progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            NSDictionary *dic = responseBody[@"data"];
            
            StatisticsModel *model = [[StatisticsModel alloc] init];
            
            if ([dic class] != [NSNull class]) {
                [model setValuesForKeysWithDictionary:dic];
            }
            [self creatSideViewByContent:[NSString stringWithFormat:@"最后更新时间:%@",model.update_time]];
            [self reloadMyView:model];
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

- (void)reloadMyView:(StatisticsModel *)model{
    
#pragma mark - 达标
    //达标店铺已铺设
    _labelDBYiPushe.text = model.merger_store_num;
    
    //今日达标店铺
    _labelDBJinriDabiao.text = model.merger_ok_num;
    
    //本月累计达标
    _labelDBBenYueLeiji.text = model.month_ok_num;
    
    if (model.merger_store_num.integerValue == 0) {
        _labelDabiaoLV.text = @"0";
    }else if (model.merger_ok_num.integerValue == 0){
        _labelDabiaoLV.text = @"0";
    }else{
        double dabiaolv = model.merger_ok_num.doubleValue/model.merger_store_num.doubleValue;
        _labelDabiaoLV.text = [NSString stringWithFormat:@"%.2lf%%",dabiaolv *100];
    }
#pragma mark - 提成
    
    //今日提成
    _labelTiChengJinRi.text = model.merger_commission;
    
    //本月累计提成
    _labelTiChengLeiJi.text = model.month_commission;
 
    
    //历史已铺店铺达标数
    _jinRiDaBiao1 = model.history_ok_num;
    
    //今日已铺店铺达标数
    _jinRiDaBiao2 = model.today_ok_num;
    
    //历史已铺店铺达标提成
    _jinRiTiCheng1 = model.history_commission;
    
    //今日已铺店铺达标提成
    _jinRiTiCheng2 = model.today_commission;
    
    //实时在线
    _labelOnline_num.text = [NSString stringWithFormat:@"%@",model.online_num];
    
    //实时离线
    _labelOffline_num.text = [NSString stringWithFormat:@"%@",model.offline_num];
    
    //平均在线
    _labelAvg_online_num.text = [NSString stringWithFormat:@"%@",model.avg_online_num];
    
#pragma mark - 新增
#pragma mark - 我的设备
    //已铺
    _labelCdbYiPuShe.text = [NSString stringWithFormat:@"%ld",model.already_wifi_num.integerValue + model.already_mima_num.integerValue];
    //今日使用
    _labelCdbZaixian.text = [NSString stringWithFormat:@"%ld",model.use_wifi_num.integerValue + model.use_mima_num.integerValue];
    //已铺WiFi设备
    _labelWodeSheBeiYongyou.text = [NSString stringWithFormat:@"%@",model.already_wifi_num];
    //使用WiFi设备
    _labelWodeShebeiShiyong.text = [NSString stringWithFormat:@"%@",model.use_wifi_num];
    //已铺密码设备
    _labelWodeShebeiMimaYongyou.text = [NSString stringWithFormat:@"%@",model.already_mima_num];
    //使用密码设备
    _labelWodeShebeiMimaShiyong.text = [NSString stringWithFormat:@"%@",model.use_mima_num];
    
#pragma mark - 我的涨粉
    
    NSInteger allZhangFen = model.wifi_sub_count.integerValue + model.mima_sub_count.integerValue;
    NSInteger allUseNum = model.use_wifi_num.integerValue + model.use_mima_num.integerValue;
    
//    allZhangFen = 10;
//    allUseNum = 3;
    
    _labelZFZongZF.text = [NSString stringWithFormat:@"%ld",allZhangFen];
    _labelZFZongPingjun.text =(allZhangFen == 0 || allUseNum == 0) ? @"0" :  [NSString stringWithFormat:@"%.2lf",(allZhangFen*1.00)/allUseNum];
    
    _labelZFWiFiZF.text = [NSString stringWithFormat:@"%@",model.wifi_sub_count];
//    _labelZFWiFiPingjun.text =  (model.already_wifi_num.integerValue == 0  || model.wifi_sub_count.integerValue == 0 )? @"0" : [NSString stringWithFormat:@"%.2lf",model.wifi_sub_count.doubleValue/model.use_wifi_num.doubleValue];
    
    if (model.use_wifi_num.integerValue == 0) {
        _labelZFWiFiPingjun.text = @"0";
    }else if (model.already_wifi_num.integerValue == 0  || model.wifi_sub_count.integerValue == 0){
        _labelZFWiFiPingjun.text = @"0";
    }else{
        _labelZFWiFiPingjun.text = [NSString stringWithFormat:@"%.2lf",model.wifi_sub_count.doubleValue/model.use_wifi_num.doubleValue];
    }
    
    _labelZFMimaZF.text = [NSString stringWithFormat:@"%@",model.mima_sub_count];
//    _labelZFMimaPingjun.text = (model.already_mima_num.integerValue == 0 || model.mima_sub_count.integerValue == 0) ? @"0" :  [NSString stringWithFormat:@"%.2lf",model.mima_sub_count.doubleValue/model.use_mima_num.doubleValue];
    
    if (model.use_mima_num.integerValue == 0) {
        _labelZFMimaPingjun.text = @"0";
    }else if (model.already_mima_num.integerValue == 0  || model.mima_sub_count.integerValue == 0){
        _labelZFMimaPingjun.text = @"0";
    }else{
        _labelZFMimaPingjun.text = [NSString stringWithFormat:@"%.2lf",model.mima_sub_count.doubleValue/model.use_mima_num.doubleValue];
    }
}

#pragma mark - 点击事件

- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//刷新数据
- (IBAction)reloadButtonClick:(UIButton *)sender {
//    kAlert(@"刷新");
    [self loadData];
}

//点击我的提成的历史明细
- (IBAction)tichengLishiButtonClick:(UIButton *)sender {
    UIStoryboard *storyboard = kMainStroyboard;
    CurveViewController *curveVC = [storyboard instantiateViewControllerWithIdentifier:@"CurveViewController"];
    curveVC.typString = @"3";
    curveVC.shopName = @"历史提成";
    curveVC.beginDate = [BGDateHelper getTimeArrayByTimeString:[BGDateHelper getTimeStempByString:kCreated havehh:YES]][0];
    [self.navigationController pushViewController:curveVC animated:YES];
}

//标题问号
- (IBAction)titleWenhaoButtonClick:(UIButton *)sender {
    NSString *message = [NSString stringWithFormat:@"1、凌晨5点，清除历史记录\n2、设备使用概念：被使用的充电宝台数\n3、单台平均 = 使用数 ÷ 涨粉数"];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    //行间距
    paragraphStyle.lineSpacing = 10.0;
    NSDictionary * attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:15.0], NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];
    [attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];
    // 下面这句话用了KVC ，将attributes的值给了alert
    [alert setValue:attributedTitle forKey:@"attributedMessage"];//attributedTitle\attributedMessage
    //end ---
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}


//达标问号
- (IBAction)dabiaoWenhaoButtonClick:(UIButton *)sender {
    [self alertVCByTitle:@"每个月底清零计算"];
}

//提成问号
- (IBAction)tichengWenHaoButtonClick:(UIButton *)sender {
    [self alertVCByTitle:@"每个月底结算后清零"];
}

//达标叹号
- (IBAction)dabiaoTanHaoButtonClick:(UIButton *)sender {
    NSString *message = [NSString stringWithFormat:@"1、今日之前已铺店铺达标数：%@\n2、今日已铺店铺达标数：%@",_jinRiDaBiao1,_jinRiDaBiao2];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    //行间距
    paragraphStyle.lineSpacing = 10.0;
    NSDictionary * attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:15.0], NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];
    [attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];
    // 下面这句话用了KVC ，将attributes的值给了alert
    [alert setValue:attributedTitle forKey:@"attributedMessage"];//attributedTitle\attributedMessage
    //end ---
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

//提成叹号
- (IBAction)tichengTanHaoButtonClick:(UIButton *)sender {
    NSString *message = [NSString stringWithFormat:@"1、今日之前已铺店铺达标的提成：%@\n2、今日已铺店铺达标的提成：%@",_jinRiTiCheng1,_jinRiTiCheng2];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    //行间距
    paragraphStyle.lineSpacing = 10.0;
    NSDictionary * attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:15.0], NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];
    [attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];
    // 下面这句话用了KVC ，将attributes的值给了alert
    [alert setValue:attributedTitle forKey:@"attributedMessage"];//attributedTitle\attributedMessage
    //end ---
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}




/**
 
 button.tag = 101 门店历史 ； 102 -> 我的历史

 */
- (IBAction)deepButtonClick:(UIButton *)sender {
    UIStoryboard *storyboard = kMainStroyboard;
    if (sender.tag == 101) {
        CurveListViewController *curveList = [storyboard instantiateViewControllerWithIdentifier:@"CurveListViewController"];
        [self.navigationController pushViewController:curveList animated:YES];
    }else{
        CurveViewController *curveVC = [storyboard instantiateViewControllerWithIdentifier:@"CurveViewController"];
        curveVC.typString = @"1";
        curveVC.shopName = @"我的历史数据";
        curveVC.beginDate = [BGDateHelper getTimeArrayByTimeString:[BGDateHelper getTimeStempByString:kCreated havehh:YES]][0];
        [self.navigationController pushViewController:curveVC animated:YES];
    }
    
}

//点击达标店铺
- (IBAction)daBiaoMingXiButtonClick:(UIButton *)sender {
//    kAlert(@"点击达标店铺");
    UIStoryboard *storyboard = kMainStroyboard;
    
    DabiaoViewController *dabiaoVC = [storyboard instantiateViewControllerWithIdentifier:@"DabiaoViewController"];
    [self.navigationController pushViewController:dabiaoVC animated:YES];
}

- (IBAction)JiantouButtonCLick:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        [self moveFuncByBOOL:_isShowHidden];
        _isShowHidden = !_isShowHidden;
    }];
}


- (void)alertVCByTitle:(NSString *)title{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - 控制移动

- (void)moveFuncByBOOL:(BOOL)isShow{
    CGFloat orginYNum ;
    CGFloat heightNum ;
    if (isShow) {
        orginYNum = 104 + 66;
        heightNum = 138 + 66;
        _layoutHeight.constant = 736;
        _imageViewJiantou.image = [UIImage imageNamed:@"JTShang.png"];
    }else{
        orginYNum = 104;
        heightNum = 138;
        _layoutHeight.constant = 670;
        _imageViewJiantou.image = [UIImage imageNamed:@"JTXia.png"];
    }
    _viewWodeMima.frame = CGRectMake(_viewWodeMima.orginX, orginYNum, _viewWodeMima.sizeWidth, _viewWodeMima.sizeHeight);
    _viewWoDeChongdianbao.frame = CGRectMake(_viewWoDeChongdianbao.orginX, _viewWoDeChongdianbao.orginY, _viewWoDeChongdianbao.sizeWidth, heightNum);
    
    _viewZhangFen.frame = CGRectMake(_viewZhangFen.orginX, _viewWoDeChongdianbao.allHeight + 10, _viewZhangFen.sizeWidth, _viewZhangFen.sizeHeight);
    
    _viewDaBiao.frame = CGRectMake(_viewDaBiao.orginX, _viewZhangFen.allHeight + 10, _viewDaBiao.sizeWidth, _viewDaBiao.sizeHeight);
    
    _viewTiCheng.frame = CGRectMake(_viewTiCheng.orginX, _viewDaBiao.allHeight + 10, _viewTiCheng.sizeWidth, _viewTiCheng.sizeHeight);
    
    _viewMendianLiShi.frame = CGRectMake(_viewMendianLiShi.orginX, _viewTiCheng.allHeight + 10, _viewMendianLiShi.sizeWidth, _viewMendianLiShi.sizeHeight);
    
    _viewWodeLishi.frame = CGRectMake(_viewWodeLishi.orginX, _viewMendianLiShi.allHeight, _viewWodeLishi.sizeWidth, _viewWodeLishi.sizeHeight);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
